
// Copyright (C) 2011 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Diagnostics;
using System.Xml;

using Derm.Render;
using Derm.Xml;
using Derm.Xml.Collada;

namespace Derm.Scene
{
	/// <summary>
	/// A render graph node used for rendering a single object.
	/// </summary>
	[DebuggerDisplay("RenderObjectGraphNode Id={Identifier} Resource={AssociatedObjectId} Model={LocalModel}")]
	public sealed class RenderObjectGraphNode : RenderGraphNode
	{
		#region Constructors

		/// <summary>
		/// Construct a RenderObjectGraphNode.
		/// </summary>
		public RenderObjectGraphNode() : base(String.Empty)
		{
			
		}

		/// <summary>
		/// Construct a RenderObjectGraphNode.
		/// </summary>
		/// <param name="identifier">
		/// A <see cref="System.String"/> that identifies this graph node.
		/// </param>
		public RenderObjectGraphNode(string identifier) : base(identifier)
		{
			
		}

		/// <summary>
		/// Construct a RenderObjectGraphNode.
		/// </summary>
		/// <param name="identifier">
		/// A <see cref="System.String"/> that identifies this graph node.
		/// </param>
		/// <param name="renderObject">
		/// A <see cref="RenderObjectBase"/> used for rendering this node. This value can be null.
		/// </param>
		public RenderObjectGraphNode(string identifier, RenderObjectBase renderObject) : this(identifier)
		{
			mRenderObject = renderObject;
		}

		#endregion

		#region Associated Object

		/// <summary>
		/// Resource identifier of the object to be rendered.
		/// </summary>
		public string AssociatedObjectId { get { return (mResourceId); } }

		/// <summary>
		/// Object rendered with this node.
		/// </summary>
		public RenderObjectBase AssociatedObject
		{
			get { return (mRenderObject); }
			set {
				// Unreference previous object, if any
				if (mRenderObject != null)
					mRenderObject.Unref();

				// Store render object
				mRenderObject = value;

				// Reference render object
				if (mRenderObject != null)
					mRenderObject.Ref();
			}
		}

		/// <summary>
		/// Resource identifier of the object to be rendered.
		/// </summary>
		private string mResourceId;

		/// <summary>
		/// Object rendered by this node.
		/// </summary>
		private RenderObjectBase mRenderObject;

		#endregion

		#region RenderGraphNode Overrides

		/// <summary>
		/// Get the type of this RenderGraphNode.
		/// </summary>
		public override RenderGraphNodeType NodeType { get { return (RenderGraphNodeType.ObjectNode); } }

		/// <summary>
		/// Render only this node.
		/// </summary>
		/// <param name="ctx">
		/// A <see cref="RenderContext"/> used for drawing this node.
		/// </param>
		protected override void RenderThisNode(RenderContext ctx)
		{
			if (mRenderObject != null)
				mRenderObject.Render(ctx);
		}

		/// <summary>
		/// Read an element of the current XML serializable.
		/// </summary>
		/// <param name="elementName">
		/// A <see cref="System.String"/> that specify the element name.
		/// </param>
		/// <param name="xmlReader">
		/// A <see cref="XmlReader"/> used for reading child element.
		/// </param>
		public override void ReadElement(string elementName, XmlReader xmlReader)
		{
			if (elementName == null)
				throw new ArgumentNullException("elementName");
			if (xmlReader == null)
				throw new ArgumentNullException("xmlReader");

			if (elementName == XmlElementResourceId) {
				mResourceId = XmlHelper.ReadXmlTextElement(xmlReader);
			} else
				base.ReadElement(elementName, xmlReader);
		}

		/// <summary>
		/// Name of the element that specify the resources rendered along with this node.
		/// </summary>
		private const string XmlElementResourceId = "ResourceId";

		#endregion
	}
}
